﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarDiscountonPremiumDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarDiscountonPremiumDB
    {
        public CarDiscountonPremiumDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarDiscountonPremium CarDiscountonPremium)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarDiscountonPremiumInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarDiscountId", CarDiscountonPremium.ProviderCarDiscountId);
            _sPara.Add("@ProviderCompanyId", CarDiscountonPremium.ProviderCompanyId);
            _sPara.Add("@Premium", CarDiscountonPremium.Premium);
            _sPara.Add("@Description", CarDiscountonPremium.Description);
            _sPara.Add("@Rate", CarDiscountonPremium.Rate);
            _sPara.Add("@RefCEUserId", CarDiscountonPremium.RefCEUserId);
            _sPara.Add("@Status", CarDiscountonPremium.Status);
            _sPara.Add("@Finfo1", CarDiscountonPremium.FInfo1);
            _sPara.Add("@Dinfo1", CarDiscountonPremium.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarDiscountonPremium GetProviderCarDiscountonPremium(Int64 ProviderCarDiscountId)
        {
            CarDiscountonPremium _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarDiscountonPremiumSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarDiscountId", ProviderCarDiscountId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarDiscountonPremium();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarDiscountonPremium GetProviderCarDiscountonPremium(Int64 ProviderCompanyId, Int32 Premium)
        {
            CarDiscountonPremium _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarDiscountonPremiumSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@Premium", Premium);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarDiscountonPremium();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarDiscountonPremium FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarDiscountonPremium myCarDiscountonPremium = new CarDiscountonPremium();
            myCarDiscountonPremium.ProviderCarDiscountId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarDiscountId"].ToString());
            myCarDiscountonPremium.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarDiscountonPremium.Premium = Convert.ToInt32(myDataTable.Rows[RowId]["Premium"].ToString());
            myCarDiscountonPremium.Description = myDataTable.Rows[RowId]["Description"].ToString();            
            myCarDiscountonPremium.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarDiscountonPremium.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarDiscountonPremium.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarDiscountonPremium.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarDiscountonPremium.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarDiscountonPremium.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarDiscountonPremium.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarDiscountonPremium;
        }
    }


}
